package com.nosweattechnet.server;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import com.nosweattechnet.datatypes.Item;

public class ItemListAsyncTask extends CommunicatorAsyncTask {
	private BasicNameValuePair tag=new BasicNameValuePair("tag","getItemList");
	private String userIDString="user_uuid";
	private String placeIDString="place_uuid";
	
	//Creates a new Aysnc task and adds the associated values to the name value pairs
	public ItemListAsyncTask(String userid,String placeid) {
		super();
		super.addNameValuePair(tag);
		super.addNameValuePair(new BasicNameValuePair(userIDString,userid));
		super.addNameValuePair(new BasicNameValuePair(placeIDString,placeid));
	}
	
	//Returns the list of Items in the ItemList by parsing the JSONArray returned from the
	//database query
	public ArrayList<Item> getItems() throws InterruptedException, ExecutionException{
		ArrayList<Item> ai=new ArrayList<Item>();
		JSONArray jarr=this.get();
		if(jarr==null){
			return ai;
		}
		for(int i=0;i<jarr.length();i++){
			try {
				ai.add(new Item(jarr.getJSONObject(i)));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return ai;
	}
}
